﻿var previousExecuteOnServerCall;
var changeTrackerDisabled;


/*****************************************************************************
 * Sends the view-model to server-side processing.
 *****************************************************************************/
executeOnServer = function (model, url) {
    executeOnServer(model, url, null);
}
executeOnServer = function (model, url, completeFunction) {
    executeOnServerCalled(model, url, completeFunction, realExecuteOnServer);
};

realExecuteOnServer = function (model, url, completeFunction) {
    changeTrackerDisabled = true;
    previousExecuteOnServerCall = new Date().getTime();

    $.ajax({
        url: url,
        type: 'POST',
        data: ko.mapping.toJSON(model),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            if (data.Redirect) {
                changeTrackerDisabled = false;
                location.href = resolveUrl(data.Url);
            }
            else {
                ko.mapping.fromJS(data, model);
                changeTrackerDisabled = false;
            }
        },
        error: function (error) {
            changeTrackerDisabled = false;
            alert("There was an error posting the data to the server: " + error.responseText);
        },
        complete: completeFunction
    });
};

resolveUrl = function (url) {
    if (url.indexOf("~/") == 0) {
        url = baseUrl + url.substring(2);
    }
    return url;
};

/*****************************************************************************
 * Occurs when execute on server has been called.
 *****************************************************************************/
executeOnServerCalled = function (model, url, completeFunction, executeFunction) {
    var now = new Date().getTime();
    if (previousExecuteOnServerCall) {
        var ticksSincePrevious = now - previousExecuteOnServerCall;
        if (ticksSincePrevious < 250) {
            return;
        }
    }

    var actionName = getActionName(url);

    switch (actionName) {
        case "StartApplicationServer":
            handleStartApplicationServer(model, url, completeFunction, executeFunction);
            break;
        case "StopApplicationServer":
            handleStopApplicationServer(model, url, completeFunction, executeFunction);
            break;
        case "RecycleInstalledApplications":
            handleRecycleInstalledApplications(model, url, completeFunction, executeFunction);
            break;
        case "StartApplication":
            handleStartApplication(model, url, completeFunction, executeFunction);
            break;
        case "StopApplication":
            handleStopApplication(model, url, completeFunction, executeFunction);
            break;
        case "RecycleApplication":
            handleRecycleApplication(model, url, completeFunction, executeFunction);
            break;
        case "UninstallApplication":
            handleUninstallApplication(model, url, completeFunction, executeFunction);
            break;
        default:
            executeFunction(model, url, completeFunction);
    }
};

/*****************************************************************************
 * Handles the 'StartApplicationServer' action.
 *****************************************************************************/
handleStartApplicationServer = function (model, url, completeFunction, executeFunction) {
    bootbox.dialog({
        title: "Start application server",
        message: "Are you sure you want to start the application server?",
        buttons: {
            success: {
                label: "Yes, start application server!",
                className: "btn-primary",
                callback: function () {
                    executeFunction(model, url, completeFunction);
                }
            },
            main: {
                label: "Cancel",
                className: "btn-default",
                callback: function () { }
            }
        }
    });
};

/*****************************************************************************
 * Handles the 'StopApplicationServer' action.
 *****************************************************************************/
handleStopApplicationServer = function (model, url, completeFunction, executeFunction) {
    bootbox.dialog({
        title: "Stop application server",
        message: "Are you sure you want to stop the application server?",
        buttons: {
            success: {
                label: "Yes, stop application server!",
                className: "btn-primary",
                callback: function () {
                    executeFunction(model, url, completeFunction);
                }
            },
            main: {
                label: "Cancel",
                className: "btn-default",
                callback: function () { }
            }
        }
    });
};

/*****************************************************************************
 * Handles the 'RecycleInstalledApplications' action.
 *****************************************************************************/
handleRecycleInstalledApplications = function (model, url, completeFunction, executeFunction) {
    bootbox.dialog({
        title: "Recycle installed applications",
        message: "Are you sure you want to recycle installed applications?",
        buttons: {
            success: {
                label: "Yes, recycle installed applications!",
                className: "btn-primary",
                callback: function () {
                    executeFunction(model, url, completeFunction);
                }
            },
            main: {
                label: "Cancel",
                className: "btn-default",
                callback: function () { }
            }
        }
    });
};

/*****************************************************************************
 * Handles the 'StartApplication' action.
 *****************************************************************************/
handleStartApplication = function (model, url, completeFunction, executeFunction) {
    bootbox.dialog({
        title: "Start application",
        message: "Are you sure you want to start the selected application?",
        buttons: {
            success: {
                label: "Yes, start the selected application!",
                className: "btn-primary",
                callback: function () {
                    executeFunction(model, url, completeFunction);
                }
            },
            main: {
                label: "Cancel",
                className: "btn-default",
                callback: function () { }
            }
        }
    });
};

/*****************************************************************************
 * Handles the 'StopApplication' action.
 *****************************************************************************/
handleStopApplication = function (model, url, completeFunction, executeFunction) {
    bootbox.dialog({
        title: "Stop application",
        message: "Are you sure you want to stop the selected application?",
        buttons: {
            success: {
                label: "Yes, stop the selected application!",
                className: "btn-primary",
                callback: function () {
                    executeFunction(model, url, completeFunction);
                }
            },
            main: {
                label: "Cancel",
                className: "btn-default",
                callback: function () { }
            }
        }
    });
};

/*****************************************************************************
 * Handles the 'RecycleApplication' action.
 *****************************************************************************/
handleRecycleApplication = function (model, url, completeFunction, executeFunction) {
    bootbox.dialog({
        title: "Recycle application",
        message: "Are you sure you want to recycle the selected application?",
        buttons: {
            success: {
                label: "Yes, recycle the selected application!",
                className: "btn-primary",
                callback: function () {
                    executeFunction(model, url, completeFunction);
                }
            },
            main: {
                label: "Cancel",
                className: "btn-default",
                callback: function () { }
            }
        }
    });
};

/*****************************************************************************
 * Handles the 'UninstallApplication' action.
 *****************************************************************************/
handleUninstallApplication = function (model, url, completeFunction, executeFunction) {
    bootbox.dialog({
        title: "Uninstall application",
        message: "Are you sure you want to uninstall the selected application?",
        buttons: {
            success: {
                label: "Yes, uninstall the selected application!",
                className: "btn-primary",
                callback: function () {
                    executeFunction(model, url, completeFunction);
                }
            },
            main: {
                label: "Cancel",
                className: "btn-default",
                callback: function () { }
            }
        }
    });
};


/*****************************************************************************
 * Handles the 'XXX' action.
 *****************************************************************************/
handleXXX = function (model, url, completeFunction, executeFunction) {
    bootbox.dialog({
        title: "XXX",
        message: "Are you sure you want to XXX?",
        buttons: {
            success: {
                label: "Yes, XXX!",
                className: "btn-primary",
                callback: function () {
                    executeFunction(model, url, completeFunction);
                }
            },
            main: {
                label: "Cancel",
                className: "btn-default",
                callback: function () { }
            }
        }
    });
};



/*****************************************************************************
 * Gets the current action method name.
 *****************************************************************************/
getActionName = function (url) {
    var urlParts = url.split('/');
    var tmp = urlParts[2];
    urlParts = tmp.split('?');
    var actionName = urlParts[0];
    return actionName;
};

/*****************************************************************************
 * Occurs when the view model has been changed.
 *****************************************************************************/
function hitChangeTracker(viewModel, newValue) {
    if (changeTrackerDisabled) {
        return;
    }
    viewModel.IsDirty(true);
};
